package com.zmn.oms.function.impl.channel;

import com.alibaba.fastjson.JSON;
import com.zmn.base.channel.common.dro.ChannelDRO;
import com.zmn.base.channel.common.query.ChannelQuery;
import com.zmn.base.channel.dubbo.interfaces.channel.ChannelListRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.function.interfaces.channel.ChannelFunction;
import com.zmn.oms.model.entity.dict.ChannelEntrustInterface;
import com.zmn.oms.services.interfaces.dict.ChannelEntrustInterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/01/10 20:29
 */
@Slf4j
@Service("channelFunction")
public class ChannelFunctionImpl implements ChannelFunction {

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private ChannelListRemoteService channelListRemoteService;
    @Resource
    private ChannelEntrustInterfaceService channelEntrustInterfaceService;

    @Override
    @SuppressWarnings("all")
    public ChannelDRO getChannelInterface(Integer channelId) {
        ChannelQuery channelInterfaceDIO = new ChannelQuery();
        channelInterfaceDIO.setChannelId(channelId);
        log.info("[Dubbo-ChannelFunction-getChannelInterface-request] ChannelInterfaceDIO=[{}]",
                JSON.toJSONString(channelInterfaceDIO));
        ResponseDTO<List<ChannelDRO>> responseDTO = channelListRemoteService.listByQuery(channelInterfaceDIO);
        log.info("[Dubbo-ChannelFunction-getChannelInterface-response] ResponseDTO<ChannelInterfaceDRO>=[{}]",
                JSON.toJSONString(responseDTO));

        if (CollectionUtil.isNullOrEmpty(responseDTO.getData())) {
            return null;
        }

        ChannelDRO channelInterfaceDRO = responseDTO.getData().get(GlobalConsts.NONE);
        if (channelInterfaceDRO.getStatus() != null && !Objects.equals(GlobalConsts.YES, channelInterfaceDRO.getStatus())) {
            return null;
        }

        return channelInterfaceDRO;
    }

    @Override
    public ChannelEntrustInterface getChannelEntrustInterface(Integer channelId) {
        return channelEntrustInterfaceService.findByChannelId(channelId);
    }
}
